//
//  MBFirstVC.m
//  房源详情-小区详情
//
//  Created by MB__Lee on 2018/5/25.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBFirstVC.h"

#import "MBCommonMethod.h"

#import "MBFirstSearchView.h" // 首页搜索框
#import "MBFirstHeaderView.h" // 头部搜索框


#import "MBFirstNewsCell.h" // 首页新闻cell
#import "BaseHouseListCell.h" // 房源cell

#import "MBSectionOneVC.h" // 第一段用wmpagecontroller

#import "MBWMLine.h"  // 分割线

#import "MBFirstSectionHeadView.h" // 段头view

#import "FirstNewModel.h" // 新闻模型

#import "MBRecoNewsVC.h" //推荐新闻
#import "MBRecoHouseVC.h" //推荐房源

#import "MBNewsDetailVC.h" //新闻详情页面
#import "MBHouseDetailVCPush.h" //房源详情页面


#import <SDCycleScrollView.h>
#import <MJExtension.h>



#define FirstPage_cellHight   115 // cell 高度
#define FirstPage_ZeroHight   171+35+5 // 第一段cell 的高度



@interface MBFirstVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)MBFirstSearchView *searchView;

//首页的tableview
@property(nonatomic,strong)UITableView *tableview;

// 推荐房源 --
@property(nonatomic,strong)NSMutableArray *houseMut;
// 推荐新闻--
@property(nonatomic,strong)NSMutableArray *newMut;


@end

//cell 复用标志
static NSString *MBFirstNewsCell_Identi = @"MBFirstNewsCell";
static NSString *BaseHouseListCell_Identi = @"BaseHouseListCell";
static NSString *Cell_class = @"Cell_class";

//段头复用标识
static NSString *MBFirstSectionHeadView_Identi = @"MBFirstSectionHeadView";

@implementation MBFirstVC

#pragma mark --- 懒加载
// tableview
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview  = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
       
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        //注册新闻 和 房源cell
        [_tableview registerNib:[UINib nibWithNibName:MBFirstNewsCell_Identi bundle:nil] forCellReuseIdentifier:MBFirstNewsCell_Identi];
        [_tableview registerNib:[UINib nibWithNibName:BaseHouseListCell_Identi bundle:nil] forCellReuseIdentifier:BaseHouseListCell_Identi];
        [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:Cell_class];
        
        //注册段头
        [_tableview registerNib:[UINib nibWithNibName:MBFirstSectionHeadView_Identi bundle:nil] forHeaderFooterViewReuseIdentifier:MBFirstSectionHeadView_Identi];
        
    
        _tableview.tableHeaderView = [[MBFirstHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    }
    return  _tableview;
}
// 头部搜索框--在导航栏上
-(MBFirstSearchView *)searchView{
    if (!_searchView) {
        MBFirstSearchView *searchBar = [MBFirstSearchView MBFirstSearchViewInitail];
        _searchView = searchBar;
        
        searchBar.frame = self.navigationController.navigationBar.bounds;
        
        __weak typeof(self) weakSelf = self;
        searchBar.cityBlock = ^(UIViewController *vc) {
            [MBCommonMethod CommonMethod_PresentOriginalViewController:weakSelf presentViewController:vc];
            
        };
        searchBar.textBlock = ^(UIViewController *vc) {
            [MBCommonMethod CommonMethod_PresentOriginalViewController:weakSelf presentViewController:vc];
        };
        
        [self.navigationController.navigationBar addSubview:searchBar];
    }
    return  _searchView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableview]; //添加tableview
    
    [self getSectionHouseSourceFromServer];//推荐房源
    [self getSectionNewsSourceFromServer];// 推荐新闻
    
}
#pragma mark -- 推荐房源
-(void)getSectionHouseSourceFromServer{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"category"] = @"1001";
    dict[@"city"] = MBCityCode;
    
    [MBNetTool MBPost:Fang_list paramater:dict success:^(id response) {
       
        if ([response[@"code"] integerValue] == 10000) {
            self.houseMut = [BaseHouseModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            [self.tableview reloadData];
            
        }else{
            
        }
        
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
#pragma mark --推荐新闻
-(void)getSectionNewsSourceFromServer{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"type"] = @"1";
    
    [MBNetTool MBPost:News_list paramater:dict success:^(id response) {
        if ([response[@"code"] integerValue] == 10000) {
            self.newMut = [FirstNewModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            [self.tableview reloadData];
        }else{
            
        }
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

#pragma mark --UITableViewDelegate-UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger rowNum = 0;
    
    if (section == 0) {
        rowNum = 1;
    }else if (section == 1){
        if (self.houseMut.count > 5) {
            rowNum = 5;
        }else{
            rowNum = self.houseMut.count;
        }
    }else if (section == 2){
        if (self.newMut.count > 5) {
            rowNum = 5;
        }else{
            rowNum = self.newMut.count;
        }
    }
    
    return  rowNum;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell_class];
        
        
        
        MBSectionOneVC *onevc = [[MBSectionOneVC alloc] init];
        onevc.view.frame = cell.bounds;
        [cell addSubview:onevc.view];
        [self addChildViewController:onevc];
        
        
        //line
        CGFloat width  = 0.25 * SCREEN_WIDTH;
        for (int i = 1 ; i< 4; i++) {
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(width * i, MBScale(10), 1, MBScale(15))];
            line.backgroundColor = MBLitterLineColor;
            [cell addSubview:line];
        }
    
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return  cell;
    }else if(indexPath.section  == 1){
        BaseHouseListCell *houseCell  = [tableView dequeueReusableCellWithIdentifier:BaseHouseListCell_Identi];
        if (self.houseMut.count) {
            houseCell.model = self.houseMut[indexPath.row];
        }
        
        
        houseCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return  houseCell;
    }else if (indexPath.section == 2){
        MBFirstNewsCell *newCell = [tableView dequeueReusableCellWithIdentifier:MBFirstNewsCell_Identi];
        if (self.newMut.count) {
           newCell.model = self.newMut[indexPath.row];
        }
        
        
        newCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return  newCell;
    }
    
    
    return  nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section  == 1) {
        

        BaseHouseModel *houseModel = self.houseMut[indexPath.row];
        MBHouseDetailVCPush *detailVC = [[MBHouseDetailVCPush alloc] init];
        detailVC.sn = houseModel.sn;
        [self.navigationController pushViewController:detailVC animated:YES];
        
    }else if (indexPath.section == 2){
        FirstNewModel *newsModel  = self.newMut[indexPath.row];
        
        MBNewsDetailVC *newsDVC = [[MBNewsDetailVC alloc] init];
        newsDVC.sn = newsModel.sn;
        newsDVC.title = @"新闻";
        
        [self.navigationController pushViewController:newsDVC animated:YES];
        
    }
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat sectionHight = 0;
    if (indexPath.section == 0) {
        sectionHight = FirstPage_ZeroHight;
    }else if(indexPath.section == 1){
        sectionHight = FirstPage_cellHight;
    }else if (indexPath.section == 2){
        sectionHight = FirstPage_cellHight;
    }
    return  sectionHight;
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    MBFirstSectionHeadView *headerView;
    if (section == 0) {
        headerView = nil;
       
    }else if (section == 1){
        headerView = [MBFirstSectionHeadView MBFirstSectionViewCreat];
        headerView.title = @"推荐房源";
        headerView.btnName = @"更多房源";
        
        
        headerView.sectionBlock = ^{
        
            MBRecoHouseVC *houseVC = [[MBRecoHouseVC alloc] init];
            [self.navigationController pushViewController:houseVC animated:YES];
            
        };
    }else if (section == 2){
        headerView = [MBFirstSectionHeadView MBFirstSectionViewCreat];
        headerView.title = @"租房新闻";
        headerView.btnName = @"更多新闻";
        headerView.sectionBlock = ^{
            MBRecoNewsVC *newsVC = [[MBRecoNewsVC alloc] init];
            [self.navigationController pushViewController:newsVC animated:YES];
        };
    }
    
    
    return  headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    CGFloat headHight = 0;
    if (section == 0) {
        headHight = 1;
    }else if (section == 1){
        headHight = 40;
    }else if (section == 2){
        headHight = 40;
    }
    return  headHight;
}




#pragma mark --SDCycleScrollViewDelegate
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --- 懒加载数组
-(NSMutableArray *)houseMut{
    if (!_houseMut) {
        _houseMut = [NSMutableArray array];
    }
    return  _houseMut;
}
-(NSMutableArray *)newMut{
    if (!_newMut) {
        _newMut = [NSMutableArray array];
    }
    return  _newMut;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
